package com.ubing.boot.notesforjava.test.chat;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.sctp.nio.NioSctpServerChannel;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;

/**
 * @ClassName : ChatServer
 * @Description : ChatServer
 * @Author : Xxxxx
 * @Date: 2023-03-26 12:38
 */
public class ChatServer {

    private int port;

    public ChatServer(int port) {
        this.port = port;
    }

    public void run() throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap(); // (2)
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class) // (3)
                    .childHandler(new ChannelInitializer<SocketChannel>() { // (4)
                        @Override
                        public void initChannel(SocketChannel ch) throws Exception {
                            ChannelPipeline pipeline = ch.pipeline();
//                            pipeline.addLast("decoder",new StringDecoder());
//                            pipeline.addLast("encoder",new StringEncoder());
                            pipeline.addLast(new ChatServerHandler());
                        }
                    })
                    .option(ChannelOption.SO_BACKLOG, 128)          // (5)
                    .childOption(ChannelOption.SO_KEEPALIVE, true); // (6)
            System.out.println("服务器启动");
            // Bind and start to accept incoming connections.
            ChannelFuture f = b.bind(port).sync(); // (7)

            // Wait until the server socket is closed.
            // In this example, this does not happen, but you can do that to gracefully
            // shut down your server.
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        int port = 8080;
        if (args.length > 0) {
            port = Integer.parseInt(args[0]);
        }

        new ChatServer(port).run();
    }






//    public static void main(String[] args) {
//        // 连接选择器
//        NioEventLoopGroup bossGroup = new NioEventLoopGroup(1);
//        // 事件选择器
//        NioEventLoopGroup workerGroup = new NioEventLoopGroup(8);
//
////        ServerBootstrap bootstrap = new ServerBootstrap();
////        bootstrap.group(bossGroup,workerGroup)
////                .channel(NioSctpServerChannel.class)
////                .option(ChannelOption.SO_BACKLOG,1024)
////                .childHandler()
//
//        try {
//            // 创建服务器端的启动对象
//            ServerBootstrap bootstrap = new ServerBootstrap();
//            // 使用链式编程来配置参数
//            bootstrap.group(bossGroup, workerGroup) //设置两个线程组
//                    // 使用NioServerSocketChannel作为服务器的通道实现
//                    .channel(NioServerSocketChannel.class)
//                    // 初始化服务器连接队列大小，服务端处理客户端连接请求是顺序处理的,所以同一时间只能处理一个客户端连接。
//                    // 多个客户端同时来的时候,服务端将不能处理的客户端连接请求,将放在队列中等待处理
//                    .option(ChannelOption.SO_BACKLOG, 1024)
//                    //创建通道初始化对象，设置初始化参数，在 SocketChannel 建立起来之前执行
//                    .childHandler(new ChannelInitializer<SocketChannel>() {
//                        @Override
//                        protected void initChannel(SocketChannel socketChannel) throws Exception {
//                            //对workerGroup的SocketChannel设置处理器
//                            socketChannel.pipeline().addLast(new NettyServerHandler());
//                        }
//                    });
//
//            System.out.println("netty server start。。");
//
//
//            ChannelFuture channelFuture = bootstrap.bind(9000).sync();
//            channelFuture.channel().closeFuture().sync();
//        } finally {
//            bossGroup.shutdownGracefully();
//            workerGroup.shutdownGracefully();
//        }
//
//
//    }
}
